package cn.com.nes.site.module.dao.crm;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.Page;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.BaseDaoImpl;

@Repository
public class DynamicDaoImpl extends BaseDaoImpl{
	private TransactionStatus newg_status = null;
	
	public PageData getDynamicList(DataReq req) {
        Map<String, Object> param = req.getReqData();
        PageData res = new PageData();
        res.setRetCode(9);
        res.setRetMessage("获取动态列表失败");
        try {
            StringBuffer sb = new StringBuffer();
            StringBuffer sbfrom = new StringBuffer();
            StringBuffer sbQuery = new StringBuffer();
            StringBuffer sbQuery1 = new StringBuffer();
            
            sbQuery.delete(0, sbQuery.length());
            sbQuery1.delete(0, sbQuery1.length());

			String userType=req.getUinfo().getUserType();
			if(!"04".equals(userType)){
				sbQuery.append(" and a.userid='").append(req.getUinfo().getId()).append("' ");
				sbQuery1.append(" and a.userid='").append(req.getUinfo().getId()).append("' ");
			}
            //操作类型 01 线索 02 客户 03 商机 04 项目
            if(!StringUtils.isEmpty((String) param.get("operType"))){
            	sbQuery.append(" and a.type='").append(param.get("operType")).append("' ");
            	sbQuery1.append(" and a.type='").append(param.get("operType")).append("' ");
            }
            //用户的userid
            if(!StringUtils.isEmpty((String) param.get("userid"))){
            	sbQuery.append(" and a.createUid='").append(param.get("userid")).append("' ");
            	sbQuery1.append(" and a.userid='").append(param.get("userid")).append("' ");
            }
            //动态类型  01  操作类动态   02  拜访类动态
            if(!StringUtils.isEmpty((String) param.get("type"))){
            	if("01".equals(param.get("type"))){
            		sbQuery.append(" and 1=0 ");
            		sbQuery1.append(" and 1=1 ");
            	}else{
            		sbQuery.append(" and 1=1 ");
            		sbQuery1.append(" and 1=0 ");
            	}
            }
            //组合查询
            if(!StringUtils.isEmpty((String) param.get("query"))){
            	sbQuery.append(" and (d.cluename like '%").append(param.get("query")).append("%' ")
            		   .append(" or e.customername like '%").append(param.get("query")).append("%' ")
            		   .append(" or f.opportunityname like '%").append(param.get("query")).append("%' ")
            		   .append(" or g.project_name like '%").append(param.get("query")).append("%' ) ");
            	sbQuery1.append(" and (c.cluename like '%").append(param.get("query")).append("%' ")
		     		    .append(" or d.customername like '%").append(param.get("query")).append("%' ")
		     		    .append(" or e.opportunityname like '%").append(param.get("query")).append("%' ")
		     		    .append(" or f.project_name like '%").append(param.get("query")).append("%' ) ");
            }
            //日期类型 
            if(!StringUtils.isEmpty((String) param.get("dateType"))){
            	String dateType = String.valueOf(param.get("dateType")) ;
            	String condition = dateTypeCondition(dateType);
            	sbQuery.append(condition);
            	sbQuery1.append(condition);
            }
            
            //操作日期 remark
            if(!StringUtils.isEmpty((String) param.get("remark"))) {
            	sbQuery1.append(" and a.remark like '%").append(param.get("remark")).append("%' ");
            }
            
            //拜访类型
            if(!StringUtils.isEmpty((String) param.get("visittype"))) {
            	sbQuery.append(" and a.visittype='").append(param.get("visittype")).append("' ");
            }
            sb.delete(0,sb.length());
            sb.append("select * " +
					" from ( " +
					" select a.uuid,'02' type, '拜访类动态' typeName,a.createUid operUserId,h.nickName operUserName,a.type opertype,b.name opertypeName, " +
					" d.uuid clueId,d.cluename clueName,d.culestate,d.clueowner,e.uuid customerId,e.customername customerName,f.uuid opportunityId,f.opportunityname opportunityName,g.uuid projectId,g.project_name projectName, " +
					" a.createTime operTime,a.username visitUserName,a.visittype visitType,c.`name` visitTypeName, " +
					" a.visitaddress visitAddress,a.visittime visitTime,a.visitcontent visitContent,'' remark,'' memo " +
					" from log_claim a " +
					" left join t_param b on a.type=b.`value` and b.param_type='18' " +
					" left join t_param c on a.visittype=c.`value` and c.param_type='19' " +
					" left join clue d on a.clueId=d.uuid " +
					" left join customer e on a.customerId=e.uuid " +
					" left join opportunity f on a.opportunityId=f.uuid " +
					" left join t_project g on a.projectId=g.uuid " +
					" left join t_user h on a.createUid=h.id " +
					" where a.isdelete=0 and a.companyId like concat('"+req.getUinfo().getCompanyId()+"','%') "); 
            		sb.append(sbQuery.toString());
					sb.append(" union  " +
					" select a.uuid,'01' type,'操作类动态' typeName,a.userid operUserId,a.username operUserName, " +
					" a.type opertype,b.name opertypeName,c.uuid clueId,c.cluename clueName,c.culestate,c.clueowner,d.uuid customerId,d.customername customerName,e.uuid opportunityId,e.opportunityname opportunityName,f.uuid projectId,f.project_name projectName, " +
					" a.createTime operTime,'' visitusername,'' visittype,'' visittypeName,'' visitaddress,'' visittime,'' visitcontent, " +
					" a.remark,a.memo " +
					" from log_operate a " +
					" left join t_param b on a.type=b.`value` and b.param_type='18' " +
					" left join clue c on a.clueId=c.uuid " +
					" left join customer d on a.customerId=d.uuid " +
					" left join opportunity e on a.opportunityId=e.uuid " +
					" left join t_project f on a.projectId=f.uuid " +
					" where 1=1 and a.companyId like concat('"+req.getUinfo().getCompanyId()+"','%') ");
            		sb.append(sbQuery1.toString());
					sb.append(" ) l ");
            sb.append("  order by l.operTime desc ");
            sb = DBUtils.sql_check(sb) ;
            sbfrom.append(sb.toString()) ;
            int idx=sbfrom.indexOf(" from ") ;
            sbfrom.delete(0, idx);
            sbfrom.insert(0, "from (select 1 ").append(") n");
            Page page = new Page(this.getJdbcTemplate_newg(),sbfrom.toString(),req.getPageid(),req.getPagesize(),res,new Object[]{}) ;
            sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize()) ;
            List<Map<String, Object>> list = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
            res.setPage(list);
            res.setRetCode(0);
            res.setRetMessage("OK");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return res;
    }
}
